//
//  DBmethod.m
//  1.8_数据库封装
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DBmethod.h"
#import "actModel.h"

@implementation DBmethod

+ (DBmethod *)sharedDataBaseMethod {
    
    static DBmethod *method = nil;
   //线程只执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        method = [[DBmethod alloc]init];
    });
    
    return method;
}

//打开数据库
-(void)openDB {
    
    //沙盒路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] ;
    
    //拼接数据库路径
    NSString *DBpath = [document stringByAppendingPathComponent:@"DouBan.sqlite"];
    NSLog(@"沙盒：%@", DBpath);
    
    //打开数据库
    int result = sqlite3_open(DBpath.UTF8String, &DouBanDB);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据库成功~💃🏻");

    }
    else {
    NSLog(@"打开数据库失败呢😱");

    }
}



- (void)createTableWithname:(NSString *)tablename {
    
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists %@(title text primary key)", tablename];
    
    int result = sqlite3_exec(DouBanDB, sqlStr.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        NSLog(@"建表成功🎉");
        
    }else {
        NSLog(@"建表失败😳");

    }
}



- (void)insertDataintotable:(NSString *)tablename :(actModel *)model {
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into %@(title) values('%@') ", tablename, model.title];
    int result = sqlite3_exec(DouBanDB, sqlStr.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        NSLog(@"插入成功~~👻");
                self.temp = 0;
    }else {
                self.temp = 1;
        NSLog(@"插入失败了😞");
    }
}




- (void)deleteDatafromtable:(NSString *)tablename :(actModel *)model {
    
    NSString *sqlStr = [NSString stringWithFormat:@"delete from %@ where title = '%@'", tablename, model.title];
    int result = sqlite3_exec(DouBanDB, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功😙");
    }else NSLog(@"删除失败😧");
  
}



- (void)updateDataintable:(NSString *)tablename :(actModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"update  %@ set title = '%@' ", tablename, model.title];
    int result = sqlite3_exec(DouBanDB, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    }else NSLog(@"更新失败");

}



- (NSMutableArray *)selectDatafromtable:(NSString *)tablename{
    
    NSString *sqlStr = [NSString stringWithFormat:@"select * from %@ ", tablename];
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(DouBanDB, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *arrModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            actModel *model = [actModel model];
            
            model.title = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
         
            [arrModel addObject:model];
            
        }
        
    }else NSLog(@"查询失败");
    
    return arrModel;
    
}








@end
